#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N=1e6+5;
int n,k[N],x,t[N],head,mii=2e9+5,res=0;
vector<pair<int,int> > p;
template <class T = int>
inline T read() {
    char c=getchar();
    bool sign=(c=='-');  
    while (c<'0'||c>'9')
        c=getchar(),sign|=(c=='-');  
    T x=0;  
    while('0'<=c&&c<='9')
        x=(x<<1)+(x<<3)+(c&15),c=getchar();   
    return sign?(~x+1):x;
}
bool cmp(pair<int,int> x,pair<int,int> y){return x.first>y.first;}
signed main(){
	freopen("contest.in","r",stdin);
	freopen("contest.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++){
		k[i]=read();
		for(int j=1;j<=k[i];j++) x=read(),p.push_back({x,i});
	}
	sort(p.begin(),p.end(),cmp);
	for(int i=0;i<p.size();i++){
		t[p[i].second]++;if(t[p[i].second]==1) res++;
		while(head<i&&(t[p[head].second]-1)!=0) t[p[head].second]--,head++;
		if(head<=i&&res==n) mii=min(p[head].first-p[i].first,mii);
	}
	cout<<mii<<endl;
	return 0;
}

